package com.szz.cardecision.controller;

import com.szz.cardecision.entity.User;
import com.szz.cardecision.mapper.UserMapper;
import jakarta.validation.constraints.NotBlank;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class AuthController {
    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;

    public AuthController(UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/register")
    public String register(Model model) {
        model.addAttribute("registerForm", new RegisterForm());
        return "register";
    }

    @PostMapping("/register")
    public String doRegister(@ModelAttribute("registerForm") RegisterForm form, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "register";
        }
        if (userMapper.findByUsername(form.getUsername()).isPresent()) {
            model.addAttribute("error", "用户名已存在");
            return "register";
        }
        User u = new User();
        u.setUsername(form.getUsername());
        u.setPasswordHash(passwordEncoder.encode(form.getPassword()));
        u.setName(form.getName());
        u.setPhone(form.getPhone());
        u.setBudgetMin(form.getBudgetMin());
        u.setBudgetMax(form.getBudgetMax());
        u.setPreferredBodyType(form.getPreferredBodyType());
        userMapper.insert(u);
        return "redirect:/login";
    }

    public static class RegisterForm {
        @NotBlank
        private String username;
        @NotBlank
        private String password;
        private String name;
        private String phone;
        private Integer budgetMin;
        private Integer budgetMax;
        private String preferredBodyType;

        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public Integer getBudgetMin() { return budgetMin; }
        public void setBudgetMin(Integer budgetMin) { this.budgetMin = budgetMin; }
        public Integer getBudgetMax() { return budgetMax; }
        public void setBudgetMax(Integer budgetMax) { this.budgetMax = budgetMax; }
        public String getPreferredBodyType() { return preferredBodyType; }
        public void setPreferredBodyType(String preferredBodyType) { this.preferredBodyType = preferredBodyType; }
    }
}